﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WpfApplicationExperimental
{
    /// <summary>
    /// this means the main grid vie 
    /// </summary>
   public interface IListView 
    {
         // System.ComponentModel.IListSource ItemsSource { get; set; }

         void UpdateList();

         void ErrorOnNewAddition(object document);
    }

    /// <summary>
    /// this is aiming at the listview secondary controls and staf 
    /// </summary>
   public interface ItemsListView 
   {
       void UpdateList();
         System .Collections .IEnumerable  ItemsSource { get; set; }
         void SetUpColumns(Type  objectType);

   }

   public class ListSecondaryPresenter<T> : SecondaryPresenter<object >
   {
     public   ItemsListView   view;


       public ListSecondaryPresenter(ItemsListView  listView)
       {
           this.view = listView;
       }

       public override void PushModelToView(object MainGridSelectedItem)
       {

           base.PushModelToView(MainGridSelectedItem);
       }

   }

   public class ListPresenter : ListSecondaryPresenter<DataAccesLayer.Employee>
   {
      protected  DataAccesLayer.EQualDataLayerFactory  dataLayer;
    //   public   MainViewPresenter mainPresenter;

       public ListPresenter(ItemsListView  view) : base(view) {
           dataLayer = new DataAccesLayer.EQualDataLayerFactory();
       }

       public  override void PushModelToView(object MainGridSelectedItem)
       {
          base.PushModelToView(MainGridSelectedItem);
       }

   }

   public class EmployeesListPresenter : ListPresenter 
   {
       public EmployeesListPresenter(ItemsListView view)
           : base(view)
       {
          
       }

       
       public   override void PushModelToView(object MainGridSelectedItem)
       {

           DAOCollection<DataAccesLayer.Employee> employees = new DAOCollection<DataAccesLayer.Employee>(GetListItems(MainGridSelectedItem as DataAccesLayer.Document));

           employees.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(employees_CollectionChanged);
           //this.Employeegrid.ItemsSource = employees;
           view.SetUpColumns( typeof (DataAccesLayer .Employee ));
           view.ItemsSource = employees;


           base.PushModelToView(MainGridSelectedItem);
       }

       void employees_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
       {
           if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
           {
               if (mainPresenter != null)
                   if (mainPresenter.MainGridSelectedItem is DataAccesLayer.Document)
                       dataLayer.GetEmployeeAccesLayer().RemoveEmployeeToBeNotified(mainPresenter.MainGridSelectedItem as DataAccesLayer.Document, e.OldItems);
           }
       }

       protected virtual IEnumerable<DataAccesLayer.Employee> GetListItems(DataAccesLayer.Document doc)
       {
           if(doc!= null )
           return dataLayer .GetEmployeeAccesLayer (). GetEmploeesDocumentNotification(doc.Id.ToString());
           return null; 
       }
   }

   public class FormsListPresenter : ListPresenter
   {

       public FormsListPresenter(ItemsListView view)
           : base(view)
       {
          
       }

       public override void PushModelToView(object MainGridSelectedItem)
       {
           DAOCollection<DataAccesLayer.Form > forms = new DAOCollection<DataAccesLayer.Form >(GetListItems(MainGridSelectedItem as DataAccesLayer.Document  ));

           forms.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(employees_CollectionChanged);
           //this.Employeegrid.ItemsSource = employees;
           view.SetUpColumns(typeof(DataAccesLayer .Form ));
           view.ItemsSource = forms;

           base.PushModelToView(MainGridSelectedItem);
       }

       void employees_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
       {
           if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
           {
               if (mainPresenter.MainGridSelectedItem is DataAccesLayer.Document)
                   dataLayer.GetDocumentAccesLayer ().RemoveFromRecordToDocument (mainPresenter.MainGridSelectedItem as DataAccesLayer.Document, e.OldItems);
           }
       }

       protected virtual IEnumerable<DataAccesLayer.Form  > GetListItems(DataAccesLayer.Document doc)
       {
           return dataLayer.GetDocumentAccesLayer ().GetDocumentFormsAndRecords (doc.Id);
       }
   }

   public class ItemsControlAdapter : ItemsListView  
   {

       public ItemsControlAdapter(System.Windows.Controls.ItemsControl  itemsControl)
       {
           this.itemsControl = itemsControl;
       }

       public System.Windows.Controls.ItemsControl itemsControl;


       #region ItemsListView Members

       public void UpdateList()
       {
           throw new NotImplementedException();
       }

       public System.Collections.IEnumerable ItemsSource
       {
           get
           {
               throw new NotImplementedException();
           }
           set
           {
               this.itemsControl.ItemsSource  = value;
           }
       }

       #endregion

       #region ItemsListView Members


       public void SetUpColumns(Type  objectType)
       {
           throw new NotImplementedException();
       }

       #endregion
   }

   public class DataGridAdapter : ItemsListView
   {
       public void SetUpColumns(Type  objectType)
       {
           this.SetUpColumns(MainDatagridColumnsGenerator.GetDataGridColumns (objectType.ToString()));
       }

       public void SetUpColumns(IEnumerable <Microsoft .Windows .Controls .DataGridColumn > cols )
       {

           this.datagrid . AutoGenerateColumns = false;
           this.datagrid . Columns.Clear();
           foreach (Microsoft.Windows.Controls.DataGridColumn col in cols )
               this.datagrid.Columns.Add(col);
       }



       public DataGridAdapter(Microsoft .Windows .Controls .DataGrid  datagrid)
       {
           this.datagrid = datagrid;
           datagrid.RowEditEnding += new EventHandler<Microsoft.Windows.Controls.DataGridRowEditEndingEventArgs>(datagrid_RowEditEnding);
           
       }

       void datagrid_RowEditEnding(object sender, Microsoft.Windows.Controls.DataGridRowEditEndingEventArgs e)
       {

       }

       public Microsoft.Windows.Controls.DataGrid datagrid;

       #region ItemsListView Members

       public void UpdateList()
       {

       }

       public System.Collections.IEnumerable ItemsSource
       {
           get
           {
               return this.datagrid.ItemsSource;
           }
           set
           {
               
               this.datagrid . ItemsSource = value;

              // SetUpColumns(typeof(DataAccesLayer.Document) );

           }
       }

       #endregion


   }

}
